<template>
  <div class="login-container">
    <!-- 导航栏开始 -->
    <van-nav-bar class="page-bav-bar" title="标题">
      <template #left>
        <van-icon name="cross" size="20px" @click="$router.back()"></van-icon>
      </template>
    </van-nav-bar>
    <!-- 导航栏结束 -->

    <!-- 表单开始 -->
    <van-form @submit="onSubmit" ref="loginFrom">
      <!-- 手机号框 -->
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="userFromRules.mobile"
        type="number"
        maxlength="11"
      >
        <template #left-icon>
          <i class="toutiao toutiao-shouji"></i>
        </template>
      </van-field>

      <!-- 验证码框 -->
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="userFromRules.code"
        type="number"
        maxlength="6"
      >
        <template #left-icon>
          <i class="toutiao toutiao-yanzhengma"></i>
        </template>
        <template #button>
          <van-count-down
            v-if="isCountDown"
            :time="time"
            format="ss s"
            @finish="isCountDown = false"
          />
          <van-button
            v-else
            class="small-btn"
            round
            size="small"
            type="default"
            native-type="button"
            @click="onSendSms"
            >发送验证码</van-button
          >
        </template>
      </van-field>

      <!-- 登录按钮 -->
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
    <!-- 表单结束 -->
  </div>
</template>

<script>
import { login, sebdSms } from '@/api/user'
export default {
  name: 'Login',
  components: {},
  props: {},
  data () {
    return {
      user: {
        mobile: '',
        code: '246810'
      },
      userFromRules: {
        mobile: [
          { required: true, message: '请填写用户名' },
          { pattern: /^1[3|5|7|8]\d{9}$/, message: '手机号格式不正确' }
        ],
        code: [
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message: '只有六位数字' }
        ]
      },
      time: 1000 * 60,
      isCountDown: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onSubmit (values) {
      console.log('submit', values)
      // 1.获取数据
      const user = this.user
      // 2.验证表单数据
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0 // 0 表示永久持续下去, 可以设置
      })

      // 3.发起网络请求
      try {
        const res = await login(user)
        // 调用 serUser 方法 存储 token
        this.$store.commit('serUser', res.data.data)
        this.$router.push('/')
        this.$toast.success('登录成功')
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.success('用户名或验证码错误')
        } else {
          this.$toast.success('未知错误')
        }
      }
      // 4.实现登录
    },
    async onSendSms () {
      try {
        await this.$refs.loginFrom.validate('mobile')
      } catch (err) {
        return console.log(err)
      }
      this.isCountDown = true
      // 发送验证码
      try {
        await sebdSms(this.user.mobile)
        this.$toast('发送成功')
      } catch (err) {
        this.isCountDown = false
        if (err.response.status === 429) {
          this.$toast('发送太频繁,请稍后..')
        } else {
          this.$toast('发送失败,请重试')
        }
      }
    }
  }
}
</script>
<style lang="less" scoped>
.toutiao {
  font-size: 37px;
}
.small-btn {
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
}
.login-btn-wrap {
  padding: 53px 28px;
  .login-btn {
    background-color: #6db4fb;
    border: 0;
  }
}
</style>
